local shunshi = fk.CreateSkill {

  name = "joy__shunshi",

  tags = {  },

}



shunshi:addEffect(fk.EventPhaseStart, {
  name = "joy__shunshi",
  anim_type = "control",
  events = {fk.EventPhaseStart, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(shunshi.name) then
      if event == fk.EventPhaseStart and not player:isNude() then
        return player.phase == Player.Start
      elseif event == fk.Damaged and not player:isNude() and player.room.current ~= player  then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
        table.insert(targets, p.id)
    end
    local tos, id = room:askForChooseCardAndPlayers(player, targets, 1, 1, ".", "#shunshi-cost", shunshi.name, true)
    if #tos > 0 then
      event:setCostData(self, {tos[1], id})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:obtainCard(event:getCostData(self)[1], event:getCostData(self)[2], false, fk.ReasonGive)
      room:addPlayerMark(player, "@joy__shunshi_drawcards", 1)
      room:addPlayerMark(player, "@joy__shunshi_slash", 1)
      room:addPlayerMark(player, "@joy__shunshi_maxcard", 1)
  end,
})
shunshi:addEffect(fk.Damaged, {
  name = "joy__shunshi",
  anim_type = "control",
  events = {fk.EventPhaseStart, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(shunshi.name) then
      if event == fk.EventPhaseStart and not player:isNude() then
        return player.phase == Player.Start
      elseif event == fk.Damaged and not player:isNude() and player.room.current ~= player  then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
        table.insert(targets, p.id)
    end
    local tos, id = room:askForChooseCardAndPlayers(player, targets, 1, 1, ".", "#shunshi-cost", shunshi.name, true)
    if #tos > 0 then
      event:setCostData(self, {tos[1], id})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:obtainCard(event:getCostData(self)[1], event:getCostData(self)[2], false, fk.ReasonGive)
      room:addPlayerMark(player, "@joy__shunshi_drawcards", 1)
      room:addPlayerMark(player, "@joy__shunshi_slash", 1)
      room:addPlayerMark(player, "@joy__shunshi_maxcard", 1)
  end,
})
shunshi:addEffect(fk.EventPhaseEnd, {
  refresh_events = { fk.EventPhaseEnd },
  can_refresh = function(self, event, target, player, data)
      return (player.phase == Player.Play and player:getMark("@joy__shunshi_slash") ~= 0) or (player.phase == Player.Discard and player:getMark("@joy__shunshi_maxcard") ~= 0)
  end,
  on_refresh = function(self, event, target, player, data)
    if player.phase == Player.Play then
      player.room:setPlayerMark(player, "@joy__shunshi_slash", 0)
    elseif player.phase == Player.Discard then
      player.room:setPlayerMark(player, "@joy__shunshi_maxcard", 0)
    end
  end,
})

shunshi:addEffect(fk.DrawNCards, {
  name = "#joy__shunshi_delay",
  mute = true,
  events = {fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@joy__shunshi_drawcards") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.n = data.n + player:getMark("@joy__shunshi_drawcards")
    player.room:setPlayerMark(player, "@joy__shunshi_drawcards", 0)
  end,
})

shunshi:addEffect("targetmod", {
  name = "#joy__shunshi_targetmod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return player:getMark("@joy__shunshi_slash")
    end
  end,
})

shunshi:addEffect("maxcards", {
  name = "#joy__shunshi_maxcards",
  correct_func = function(self, player)
    return player:getMark("@joy__shunshi_maxcard")
  end,
})

shunshi:addEffect(fk.AfterCardTargetDeclared, {
  name = "#joy__shunshi_armor",
  mute = true,
  --shunshi,
  events = {fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill("joy__shunshi") and player.phase == Player.Play and player:getMark("@joy__shunshi_slash") > 0 then
      return data.card.trueName == "slash" 
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    card_event.joy__shunshi_armor = true
    for _, p in ipairs(room.alive_players) do
      room:addPlayerMark(p, fk.MarkArmorNullified)
    end
  end,
})
shunshi:addEffect(fk.CardUseFinished, {
  refresh_events = {fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
  if player ~= target then return false end
  local card_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
  return card_event and card_event.joy__shunshi_armor
  end,
  on_refresh = function(self, event, target, player, data)
  local room = player.room
  for _, p in ipairs(room.alive_players) do
    room:removePlayerMark(p, fk.MarkArmorNullified)
  end
  end,
})

return shunshi